package com.jiangcheng.demo.controller;


import com.jiangcheng.demo.entity.User;
import com.jiangcheng.demo.service.UserService;
import com.jiangcheng.demo.service.impl.UserServiceImpl;
import com.jiangcheng.demo.utils.EmailVo;
import com.jiangcheng.demo.utils.NewResult;
import com.jiangcheng.demo.utils.Result;
import com.jiangcheng.demo.utils.UserPicResult;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-07-15
 */
@Api("用户相关API")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result login(User user){
        System.out.println(user);
        Result result = userService.login(user);
        System.out.println(result);
        return result;
    }

    @PostMapping("/register")
    public Result register(User user){
        return userService.register(user);
    }

    @GetMapping("/findUserById/{id}")
    public NewResult findUseById(@PathVariable("id") Integer id) {
        return userService.findUserById(id);
    }

    @PostMapping("/updateUserPic")
    public NewResult updateUserPic(@Param("picPath")String picPath,@Param("uId") Integer uId) {
        System.out.println(picPath);
        return userService.updateUserPicById(picPath,uId);
    }

    // 发送邮箱
    @PostMapping("/sendEmail")
    public String sendEmail(@Param("email") String email, HttpSession httpSession, HttpServletRequest httpServletRequest) {
        System.out.println(httpSession.isNew());
        System.out.println("controller:  " + email);
        String sessionId = userService.sendMimeMail(email, httpSession);
        Cookie[] cookies = httpServletRequest.getCookies();
        System.out.println(cookies);

        return sessionId;
    }

    @PostMapping("/emailRegister")
    public Result emailRegister(EmailVo emailVo, HttpSession httpSession){
        System.out.println(httpSession.isNew());
        System.out.println(emailVo.getCode());
        System.out.println(httpSession.getAttribute("code"));
        Result result;
        if (emailVo.getCode().equals(httpSession.getAttribute("code"))){
            // 验证通过
            // 写入数据库
            User user = new User();
            BeanUtils.copyProperties(emailVo, user);
            System.out.println(user);
            result = userService.register(user);
        }else {
            result = new Result(400,false,"验证码错误");
        }
        return result;
    }

    @PostMapping("/emailLogin")
    public Result emailLogin(User user){
        System.out.println(user);
        Result result = userService.emailLogin(user);
        System.out.println(result);
        return result;
    }

    @PostMapping("/updateUsername")
    public NewResult updateUsername(@Param("username") String username,@Param("uId") Integer uId){
        return userService.updateUsernameById(username,uId);
    }

    @PostMapping("/updatePassword")
    public NewResult updatePassword(@Param("password") String password,@Param("uId") Integer uId){
        return userService.updatePasswordByUid(password,uId);
    }

}

